﻿/*******************************************************
 * 
 * 作者：胡庆访
 * 创建时间：20130326 15:35
 * 说明：此文件只包含一个类，具体内容见类型注释。
 * 运行环境：.NET 4.0
 * 版本号：1.0.0
 * 
 * 历史记录：
 * 创建文件 胡庆访 20130326 15:35
 * 
*******************************************************/

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;

namespace MSDNConverter
{
    class Program
    {
        static void Main(string[] args)
        {
            var rootId = ConfigurationManager.AppSettings["RootId"];
            var excludeSubIdList = ConfigurationManager.AppSettings["ExcludeSubIdList"] ?? string.Empty;

            var name = typeof(Program).Assembly.GetName();
            Console.WriteLine(string.Format(@"
====================================================================
            MSDN To Word Utility({2})
                                Author:HuQingfang
====================================================================
The root id of the article you want to download: {0},
The id list which will be excluded from downloading: {1}.
press enter to start.", rootId, excludeSubIdList, name.Version));
            Console.ReadLine();

            var reader = new CacheReader { RootId = rootId };
            foreach (var excludeId in excludeSubIdList.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                var value = excludeId.Trim();
                if (!reader.Excludes.Contains(value)) { reader.Excludes.Add(value); }
            }

            //Read
            var raw = reader.ReadAllArticle();

            //Convert
            var converter = new HtmlConverter(reader);
            var article = converter.Convert(raw);

            //Write
            Console.WriteLine(@"start writing to word....
DON'T EDIT Word until this process is finished !");
            var writer = new WordWriter { Article = article };
            writer.Write();
        }
    }
}